package com.seecen.day07;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BallJPanel extends JPanel implements Runnable, MouseMotionListener, MouseListener {
    //属性：玩家飞机，无限敌机，无限子弹，很多星星...
    //玩家飞机坐标
    public int x;
    public int y;
    //获取英雄照片
    public Image img_hero = new ImageIcon("feiji.jpg").getImage();

    //星星坐标
    public int[] xx;
    public int[] yy;
    //星星的大小
    public Font[] fs;
    //无限的子弹
    public List<Ball> Fires;
    //无限的敌机
    public List<Ball> Foes;
    //英雄是否发射子弹
    public boolean isFire;

    /**
     * 通过无参构造给所有属性赋值
     *
     * @param
     */
    public BallJPanel() {
        //英雄
        x = 420;
        y = 900;
        //星星
        xx = new int[300];
        yy = new int[300];
        fs = new Font[5];
        for (int i = 0; i < xx.length; i++) {
            //随机生成星星的X,Y坐标
            xx[i] = new Random().nextInt(800);
            yy[i] = new Random().nextInt(1000);
        }
        for (int i = 0; i < fs.length; i++) {
            fs[i] = new Font("宋体", 1, new Random().nextInt((10) + 15));
        }
        //无限的敌机
        Foes = new ArrayList<Ball>();
        //无限的子弹
        Fires = new ArrayList<Ball>();

        //添加鼠标监听
        this.addMouseListener(this);
        this.addMouseMotionListener(this);

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //设置背景颜色
        this.setBackground(Color.BLACK);
        //绘制英雄
        g.drawImage(img_hero, x, y, 80, 80, null);
        //画星星
        for (int i = 0; i < xx.length; i++) {
            //设置星星颜色
            g.setColor(new Color(new Random().nextInt(0xffffff)));//设置星星随机颜色bound随机范围，0xffffff是最大值
            //设置星星的大小
            g.setFont(fs[new Random().nextInt(fs.length)]);
            //画星星
            g.drawString("*", xx[i], yy[i]);
        }
        //绘制所有的敌机
        for (int i = 0; i < Foes.size(); i++) {
            Ball ball = Foes.get(i);
            ball.draw(g);
        }
        //绘制所有的子弹
        for (int i = 0; i < Fires.size(); i++) {
            Ball ball = Fires.get(i);
            ball.draw(g);
        }
    }
    @Override
    public void run() {

    }
    @Override
    public void mouseClicked(MouseEvent e) {

    }

    /**
     * 鼠标按压触发
     *
     * @param e
     */
    @Override
    public void mousePressed(MouseEvent e) {
        isFire = true;
    }

    /**
     * 鼠标释放触发
     *
     * @param e
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        isFire = false;
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        x = e.getX() - 20;
        y = e.getY() - 20;

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        x = e.getX() - 20;
        y = e.getY() - 20;
        repaint();

    }
    public void starRun(){
        //通过线程，实现画板刷新
        Thread thread=new Thread(){
            @Override//将自己的run方法调用
            public void run() {
                int count=0;
                super.run();
                while (true){
                    count++;
                    if(count>=Integer.MAX_VALUE){
                        count=0;
                    }
                    //计数器，让线程处于休眠状态（产生间隔效果）
                    try {
                        Thread.sleep(40);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    //敌机的添加，绘制敌机
                    if (count%30==0){
                        Ball ball=new Ball();
                        ball.x=new Random().nextInt(800);
                        ball.y=-40;
                        ball.r=20;
                        ball.ori=Ball.DOWN;
                        ball.type=Ball.FOE;
                        Foes.add(ball);

                    }
                    //敌机下落
                    for (int i=0;i<Foes.size();i++){
                        Ball ball=Foes.get(i);
                        ball.move();
                        if (ball.y>=1000){
                            Foes.remove(i);
                        }
                    }
                    //绘制子弹
                    if (count%5==0&&isFire){
                        Ball ball=new Ball();
                        ball.x=x+5;
                        ball.y=y-20;
                        ball.r=20;
                        ball.ori=Ball.UP;
                        ball.type=Ball.FIRE;
                        Fires.add(ball);

                    }
                    //让子弹移动
                    for (int i=0;i<Fires.size();i++){
                        Ball ball=Fires.get(i);
                        ball.move();
                        if (ball.y<=0){
                            Fires.remove(i);
                        }
                    }
                    //判断是否发生碰撞
                    for (int i=0;i<Fires.size();i++){
                        for(int j=0;j<Foes.size();j++){
                            if (i<Fires.size()&&j<Foes.size()&&Fires.get(i).isTouch(Foes.get(j))){
                                //如果碰撞，则子弹和敌机都消失
                                Fires.remove(i);
                                Foes.remove(j);
                            }
                        }
                    }
                    //刷新画板
                    repaint();

                }

            }//RUN的结束
        };//整个对象创建的结束
        thread.start();//启动线程

    }


}
